extends Window

var src_code

const GENFUNCTIONS : Array = [ "generate_shadertoy", "generate_godot_canvasitem", "generate_godot_spatial" ]

func _on_ready() -> void:
	content_scale_factor = mm_globals.main_window.get_window().content_scale_factor
	min_size = Vector2(500 ,500) * content_scale_factor

func show_code(s) -> void:
	src_code = s
	_on_ShaderType_item_selected(0)
	if ! is_connected("popup_hide", Callable(self, "queue_free")):
		connect("popup_hide", Callable(self, "queue_free"))
	hide()
	popup_centered()

func _on_ShaderType_item_selected(index):
	if index < 3:
		$VBoxContainer/Code.visible = true
		$VBoxContainer/Code.text = call(GENFUNCTIONS[index])
		$VBoxContainer/ColorRect.visible = false
	else:
		$VBoxContainer/Code.visible = false
		$VBoxContainer/ColorRect.visible = true
		var shader : Shader = Shader.new()
		shader.code = generate_godot_canvasitem()
		$VBoxContainer/ColorRect.material.shader = shader

func _on_CopyToClipboard_pressed():
	DisplayServer.clipboard_set($VBoxContainer/Code.text)

func generate_shadertoy() -> String:
	var code = "/* Generated by Material Maker */\n\n"
	code += "#define SEED_VARIATION 0.0\n\n"
	code += mm_renderer.common_shader
	code += "\n"
	code = code.replace("varying float elapsed_time;", "")
	code = code.replace("void vertex() {\n\telapsed_time = TIME;\n}", "")
	code += src_code.uniforms_as_strings("const", true)
	code += "\n"
	code += src_code.get_globals_string(src_code.defs+src_code.code)
	var code_defs = src_code.defs
	code += code_defs
	code += "\n"
	code += "void mainImage(out vec4 fragColor, in vec2 fragCoord) {\n"
	code += "float minSize = min(iResolution.x, iResolution.y);\n"
	code += "float _seed_variation_ = SEED_VARIATION;\n"
	code += "vec2 UV = vec2(0.0, 1.0) + vec2(1.0, -1.0) * (fragCoord-0.5*(iResolution.xy-vec2(minSize)))/minSize;\n"
	code += src_code.code
	if src_code.output_values.has("rgba"):
		code += "fragColor = vec4(mix(vec3(0.5), "+src_code.output_values.rgba+".rgb, "+src_code.output_values.rgba+".a), 1.0);\n"
	else:
		code += "fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
	code += "}\n"
	#print("GENERATED SHADER:\n"+shader_code)
	code = code.replace("uniform", "const")
	code = code.replace("const sampler2D", "uniform sampler2D")
	code = code.replace("elapsed_time", "iTime")
	return code

func generate_godot_canvasitem() -> String:
	var code = "shader_type canvas_item;\n"
	code += "const float seed_variation = 0.0;\n"
	code += mm_renderer.common_shader
	code += src_code.uniforms_as_strings("uniform", true)
	code += "\n"
	code += src_code.get_globals_string(src_code.defs+src_code.code)
	code += src_code.defs
	code += "\n"
	code += "void fragment() {\n"
	code += "float _seed_variation_ = seed_variation;\n"
	code += src_code.code
	if src_code.output_values.has("rgba"):
		code += "COLOR = "+src_code.output_values.rgba+";\n"
	else:
		code += "COLOR = vec4(1.0, 0.0, 0.0, 1.0);\n"
	code += "}\n"
	#print("GENERATED SHADER:\n"+shader_code)
	code = code.replace("uniform", "const")
	code = code.replace("const sampler2D", "uniform sampler2D")
	return code

func generate_godot_spatial() -> String:
	var code = "shader_type spatial;\n"
	code += "\nconst float seed_variation = 0.0;\n"
	code += mm_renderer.common_shader
	code += "\n"
	code += src_code.uniforms_as_strings("uniform", true)
	code += "\n"
	code += src_code.get_globals_string(src_code.defs+src_code.code)
	code += src_code.defs
	code += "\n"
	code += "void fragment() {\n"
	code += "float _seed_variation_ = seed_variation;\n"
	code += src_code.code
	if src_code.output_values.has("rgb"):
		code += "ALBEDO = "+src_code.output_values.rgb+";\n"
	else:
		code += "ALBEDO = vec3(1.0, 0.0, 0.0);\n"
	code += "}\n"
	#print("GENERATED SHADER:\n"+shader_code)
	code = code.replace("uniform", "const")
	code = code.replace("const sampler2D", "uniform sampler2D")
	return code
